﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;

public partial class Supplier_Vehicle_AddEdit : System.Web.UI.Page
{
    private SupplierDataContext supplierContext = new SupplierDataContext();

    private int SupplierId
    {
        get { return Helper.QSIntValue("sid"); }
    }
    private int SupplierVehicleId
    {
        get { return Helper.QSIntValue("id"); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.SupplierVehicle;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        if (!IsPostBack)
        {
            LoadSuppliers();
            LoadVehicleTypes();
            trSupplier.Visible = UserAccess.IsCurrentUser(Enums.UserTypes.Admin);

            if (SupplierVehicleId > 0)
            {
                SupplierVehicle suppliervehicles = supplierContext.SupplierVehicles.SingleOrDefault(obj => obj.SupplierVehicleId == SupplierVehicleId);

                //CONTROL ASSIGNMENT
                ddlSupplier.SelectedValue = suppliervehicles.SupplierId.Value.ToString();
                txtVehicleCategory.Text = suppliervehicles.VehicleCategory;
                txtVehicleCode.Text = suppliervehicles.VehicleCode;
                txtVehicleDescription.Text = suppliervehicles.VehicleDescription;
                txtAdultCapacity.Text = suppliervehicles.AdultCapacity.Value.ToString();
                txtChildrenCapacity.Text = suppliervehicles.ChildrenCapacity.Value.ToString();
                txtLaggageCapacity.Text = suppliervehicles.LaggageCapacity.Value.ToString();
                txtMinAge.Text = suppliervehicles.MinAge.Value.ToString();
                txtMaxAge.Text = suppliervehicles.MaxAge.Value.ToString();
                txtLeadTime.Text = suppliervehicles.LeadTime.Value.ToString();
                ddlVehicleType.SelectedValue = suppliervehicles.VehicleTypeId.Value.ToString();
                if (suppliervehicles.AutoTransmission.HasValue)
                    chkAutoTransmission.Checked = suppliervehicles.AutoTransmission.Value;
                if (suppliervehicles.NoOfDoors.HasValue)
                    txtNoOfDoors.Text = suppliervehicles.NoOfDoors.Value.ToString();
                if (suppliervehicles.AirCondition.HasValue)
                    chkAirCondition.Checked = suppliervehicles.AirCondition.Value;
            }

        }
    }
    private void LoadVehicleTypes()
    {
        ddlVehicleType.DataSource = supplierContext.VehicleTypes;
        ddlVehicleType.DataTextField = "VehicleTypeName";
        ddlVehicleType.DataValueField = "VehicleTypeId";
        ddlVehicleType.DataBind();

        ddlVehicleType.Items.Insert(0, new ListItem("Select Vehile Type", "0"));
    }

    private void LoadSuppliers()
    {
        ddlSupplier.DataSource = supplierContext.Suppliers;
        ddlSupplier.DataTextField = "SupplierName";
        ddlSupplier.DataValueField = "SupplierId";
        ddlSupplier.DataBind();

        ddlSupplier.Items.Insert(0, new ListItem("Select Supplier", "0"));

        if (!trSupplier.Visible)
            ddlSupplier.SelectedValue = UserAccess.CurrentSupplier.SupplierId.ToString();
        else
            ddlSupplier.SelectedValue = SupplierId.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SupplierVehicle suppliervehicle = new SupplierVehicle();
        if (SupplierVehicleId > 0)
            suppliervehicle = supplierContext.SupplierVehicles.SingleOrDefault(obj => obj.SupplierVehicleId == SupplierVehicleId);

        suppliervehicle.SupplierId = Convert.ToInt32(ddlSupplier.SelectedValue);
        suppliervehicle.VehicleCategory = txtVehicleCategory.Text;
        suppliervehicle.VehicleCode = txtVehicleCode.Text;
        suppliervehicle.VehicleDescription = txtVehicleDescription.Text;
        suppliervehicle.AdultCapacity = Convert.ToInt32(txtAdultCapacity.Text);
        suppliervehicle.ChildrenCapacity = Convert.ToInt32(txtChildrenCapacity.Text);
        suppliervehicle.LaggageCapacity = Convert.ToInt32(txtLaggageCapacity.Text);
        suppliervehicle.MinAge = Convert.ToInt32(txtMinAge.Text);
        suppliervehicle.MaxAge = Convert.ToInt32(txtMaxAge.Text);
        suppliervehicle.LeadTime = Convert.ToInt32(txtLeadTime.Text);
        suppliervehicle.VehicleTypeId = Convert.ToInt32(ddlVehicleType.SelectedValue);
        suppliervehicle.AutoTransmission = chkAutoTransmission.Checked;
        suppliervehicle.NoOfDoors = Convert.ToInt32(txtNoOfDoors.Text);
        suppliervehicle.AirCondition = chkAirCondition.Checked;

        if (SupplierVehicleId == 0)
            supplierContext.SupplierVehicles.InsertOnSubmit(suppliervehicle);

        supplierContext.SubmitChanges();

        Response.Redirect("default.aspx");

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
}
